package cn.zebra.dev.admin.module.login;

import cn.zebra.dev.admin.module.security.entity.AdminDetailsEntity;
import cn.zebra.dev.base.module.admin.entity.BaseAdmin;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * Spring Security 后台用户枢纽服务层
 *
 * @author runnable@sina.cn
 */
@Service
public class SecurityAdminService {

    /**
     * 获取当前登录用户信息(所有)
     *
     * @return Security用户对象
     */
    public AdminDetailsEntity getAdmin() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        return (AdminDetailsEntity) authentication.getPrincipal();
    }

    public BaseAdmin getBaseAdmin() {
        return getAdmin().getAdmin();
    }

    /**
     * 获取当前登录用户Id
     *
     * @return 用户id
     */
    public Long getId() {
        return getBaseAdmin().getId();
    }

    /**
     * 获取当前登录用户名
     *
     * @return 用户名
     */
    public String getUserName() {
        return getAdmin().getUsername();
    }

}
